﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SaleStat.BLL.PM;
using SaleStat.COM.Constant;
public partial class PM_List : BasePage
{
    #region 初始化
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                //用户姓名焦点设置
                txtSalesName.Focus();
                getProductInfo();
                string strUserID = Session[SessionKeyConstant.SESSION_USERID].ToString();
                hdnUserID.Value = strUserID;

            }
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);

        }

    }
    #endregion

    #region 事件方法
    #region 检索按钮事件
    /// <summary>
    /// 检索产品信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgSearch_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            getProductInfo();
        }
        catch (Exception ex)
        {
            ViewState["dtCustomerInfo"] = null;
            gvProductInfo.DataSource = null;
            gvProductInfo.DataBind();
            ExceptionHandler(ex);

        }


    }
    #endregion

    #region 重置按钮事件
    /// <summary>
    /// 重置按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgReset_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            //检索条件置空
            txtSalesName.Text = "";
            txtSaleID.Text = "";
            ViewState["dtProductInfo"] = null;
            //girdview清空
            gvProductInfo.DataSource = null;
            gvProductInfo.DataBind();
            txtSalesName.Focus();
            lblMsg.Visible = false;
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }
    }
    #endregion

    #region 行绑定事件
    /// <summary>
    /// 行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvProductInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            //当前行是数据行的时候 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标放上去显高亮
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='lightcyan';" + "this.style.cursor='hand'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                string strCustomerName = e.Row.Cells[1].Text;
                //删除linkButton
                LinkButton lbtnDelete = (LinkButton)e.Row.FindControl("lnkBtnDelete");
                lbtnDelete.CommandArgument = e.Row.RowIndex.ToString();
                //修改linkButton
                LinkButton lbtnModify = (LinkButton)e.Row.FindControl("lnkBtnModify");
                //index linkButton
                LinkButton lbtnIndex = (LinkButton)e.Row.FindControl("lbtnIndex");

                //产品名称大于15位时显示14位+"..."
                if (strCustomerName.Length > 14)
                {
                    e.Row.Cells[1].Text = strCustomerName.Substring(0, 14) + "...";
                    e.Row.Cells[1].Attributes.Add("title", strCustomerName);
                }

                // 备注信息大于15位时显示14位+"..."
                string strNote = e.Row.Cells[5].Text;
                if (strNote.Length > 13)
                {
                    e.Row.Cells[5].Text = strNote.Substring(0, 13) + "...";
                    e.Row.Cells[5].Attributes.Add("title", strNote);
                }

                HiddenField hdCpID = (HiddenField)e.Row.FindControl("hdCpID");
                string strCpID = hdCpID.Value.ToString();
                string strUserID = hdnUserID.Value.ToString();
                //修改窗口打开路径 
                string strUrl = "PmInfo_Edit.aspx?CpID=" + strCpID + "&flag=1" + "&userID=" + strUserID;
                //修改窗口打开样式 
                string strOpenType = "resizable:no;scroll:no;dialogHeight:500px;dialogWidth:485px;center;status:no;help:no;";
                //index窗口打开路径
                string strIndexUrl = "PmInfo_Detail.aspx?CpID=" + strCpID;
                string strIndexOpenType = "resizable:no;scroll:no;dialogHeight:430px;dialogWidth:455px;center;status:no;help:no;";
                lbtnIndex.Attributes.Add("onclick", "return openNewWindows('" + strIndexUrl + "','" + strIndexOpenType + "')");
                //删除状态数据库到页面的显示
                lbtnModify.Attributes.Add("onclick", "return openNewWindows('" + strUrl + "','" + strOpenType + "')");
                lbtnDelete.Attributes.Add("onclick", "javaScript:return confirmDelete();");
            }
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }
    }
    #endregion

    #region 显示条数选择事件
    /// <summary>
    /// 显示条数选择事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpShowLine_SelectedIndexChanged(object sender, EventArgs e)
    {
        //pagesize位dropdownlist中选择的值
        gvProductInfo.PageSize = Convert.ToInt32(drpShowLine.SelectedValue.Trim());
        //如果没有数据
        if (ViewState["dtProductInfo"] == null)
        {
            return;
        }
        gvProductInfo.PageIndex = 0;
        gvProductInfo.DataSource = (DataTable)ViewState["dtProductInfo"];
        gvProductInfo.DataBind();
    }
    #endregion

    #region 分页按钮事件
    /// <summary>
    /// 分页按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvProductInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            if (ViewState["dtProductInfo"] == null)
            {
                return;
            }
            //pageindex为默认值
            gvProductInfo.PageIndex = e.NewPageIndex;
            gvProductInfo.PageSize = Convert.ToInt32(drpShowLine.SelectedValue);
            gvProductInfo.DataSource = (DataTable)ViewState["dtCustomerInfo"];
            gvProductInfo.DataBind();

        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }
    }
    #endregion

    #region ROW_command事件
    /// <summary>
    /// ROW_command事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvProductInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (!"Page".Equals(e.CommandName))
            {
                if ("deleteCustomerInfo".Equals(e.CommandName))
                {
                    //当前行索引取得
                    string strRow = e.CommandArgument.ToString();
                    int intIndex = Convert.ToInt32(strRow);
                    //行版本的取得
                    HiddenField hdnRowVer = (HiddenField)gvProductInfo.Rows[intIndex].Cells[6].FindControl("hdnVersion");
                    string strRowVersion = hdnRowVer.Value;
                    HiddenField hdnCpID = (HiddenField)gvProductInfo.Rows[intIndex].Cells[6].FindControl("hdCpID");
                    string strCpID = hdnCpID.Value;
                    PM_List_BLL PM_ListService = new PM_List_BLL();
                    PM_ListService.deleteCustomInfo(strCpID, strRowVersion);
                    Alert("删除成功!");
                    getProductInfo();
                    //写删除日志
                    f_Info(CommonConstant.OPERATIONS.DELETE, "删除产品信息");

                }
            }
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }

    }
    #endregion

    #endregion

    #region 自定义方法

    #region 检索产品信息
    /// <summary>
    /// 检索产品信
    /// </summary>
    protected void getProductInfo()
    {

        PM_List_BLL pmService = new PM_List_BLL();
        DataTable dtProductInfo = pmService.getCustomerInfo(getSearchCondition());
        if (dtProductInfo == null || dtProductInfo.Rows.Count == 0)
        {
            lblMsg.Visible = true;
            img.Visible = true;
            lblMsg.Text = "没有检索到相关数据！";
        }
        else
        {
            //有数据信息框不显示，绑定数据到gridview
            lblMsg.Visible = false;
            img.Visible = false;
        }
        gvProductInfo.PageIndex = 0;
        gvProductInfo.DataSource = dtProductInfo;
        gvProductInfo.DataBind();
        ViewState["dtProductInfo"] = dtProductInfo;
    }
    #endregion

    #region 检索条件的封装
    /// <summary>
    /// 检索条件的封装
    /// </summary>
    /// <returns>ArrayList</returns>
    protected ArrayList getSearchCondition()
    {
        ArrayList arrParamlist = new ArrayList();
        //产品名称 || 产品编号
        string strProductName = txtSalesName.Text.Trim();
        string strProductId = txtSaleID.Text.Trim();
        if (strProductName == "")
        {
            arrParamlist.Add("");
        }
        else
        {
            arrParamlist.Add(strProductName);
        }
        if (strProductId == "")
        {
            arrParamlist.Add("");
        }
        else
        {
            arrParamlist.Add(strProductId);
        }
        return arrParamlist;
    }
    #endregion
    #endregion
}
